import type { components } from '@/genapi/wh';
import { whManagerBsTbEqpGetByIdIdUsingPost,whManagerBsTbEqpGetPageUsingPost } from '@/genapi/wh';
import { usePage } from '@/hooks/use-page';
// import { useCompanyStore } from '@/stores/company';
import { useUserStore } from '@/stores/user';

export const [createState, injectState] = createInjectionState(() => {
  // const companyStore = useCompanyStore();
  const userStore = useUserStore();

  const filter = ref<components['schemas']['TbEqpQueryDTO']>();

  const page = usePage({
    initPageSize: 15,
    fetch: async ({ pageSize, currentPage }) => {
      const { data } = await whManagerBsTbEqpGetPageUsingPost({
        // data: {
        //   size: pageSize,
        //   current: currentPage,
        //   query: {
        //     enterpriseId: companyStore.companyInfo?.id,
        //     ...filter.value,
        //   },
        // },
        size: pageSize,
        current: currentPage,
        query: { enterpriseId: userStore.userInfo?.systemUser?.relatedId }
      });
      return {
        records: data?.records ?? [],
        total: data?.total ?? 0,
        pageSize,
      };
    },
  });

  watchDeep(filter, () => page.execute({ currentPage: 0 }));

  // 当前选中的列表项
  const selectedList = shallowRef<any[]>([]);

  return {
    page,
    filter,
    selectedList,
  };
});
